﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CacheManager.Core;
using GrayParrot.Caching;
using GrayParrot.Caching.MemoryCache.Configuration;

namespace GrayParrot.Caching
{
    public class Cache : CacheBase, ICache
    {
        private ICacheManager<Object> m_cache = null;

        public Cache()
        {

        }

        public static Cache Create()
        {
            Cache retValue = new Cache();
            retValue.Initialize();

            return retValue;
        }

        public override void Initialize()
        {
            TimeSpan m_timeout = this.InitializeExpirationPeriod();

            m_cache = CacheFactory.Build("GrayParrot.Caching.MemoryCache", settings =>
            {
                settings.WithSystemRuntimeCacheHandle("GrayParrot.Caching.MemoryCache")
                    .WithExpiration(ExpirationMode.Sliding, m_timeout);
                    
            });


            string m_pippolo = this.Configuration.GetCustomSetting();
        }

        public void Add<T>(string key, T value)
        {
            if (m_cache != null)
            {
                m_cache.Add(key, value);
            }
        }

        public void Add<T>(string key, T value, DateTimeOffset expiration)
        {
            if (m_cache != null)
            {
                m_cache.Expire(key, expiration);
                m_cache.Add(key, value);
            }
        }

        public void Add<T>(string key, string region, T value)
        {
            if (m_cache != null)
            {
                m_cache.Add(key, value, region);
            }
        }

        public void Add<T>(string key, string region, T value, DateTimeOffset expiration)
        {
            if (m_cache != null)
            {
                m_cache.Expire(key, region, expiration);
                m_cache.Add(key, value, region);
            }
        }

        public void Update<T>(string key, T value)
        {
            if (m_cache != null)
            {
                m_cache.Update(key, x => value);
            }
        }

        public void Update<T>(string key, string region, T value)
        {
            if (m_cache != null)
            {
                m_cache.Add(key, value);
            }
        }

        public void Remove(string key)
        {
            if (m_cache != null)
            {
                m_cache.Remove(key);
            }
        }

        public void Remove(string key, string region)
        {
            if (m_cache != null)
            {
                m_cache.Remove(key, region);
            }
        }

        public void Clear()
        {
            if (m_cache != null)
            {
                m_cache.Clear();
            }
        }

        public void ClearRegion(string region)
        {
            if (m_cache != null)
            {
                m_cache.ClearRegion(region);
            }
        }

        public T Get<T>(string key)
        {
            T retValue = default(T);

            if (m_cache != null)
            {
                retValue = m_cache.Get<T>(key);
            }

            return retValue;
        }

        public T Get<T>(string key, string region)
        {
            T retValue = default(T);

            if (m_cache != null)
            {
                retValue = m_cache.Get<T>(key, region);
            }

            return retValue;
        }
    }
}
